package com.fullhome8.cn.wanghongweb.redis;


import com.fullhome8.cn.wanghongweb.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;

@Service
@Slf4j
public class RedisService {

    @Value("${application.redis.expireTime}")
    private int expireTime; 

    @Value("${application.redis.prefix}")
    private String prefix;

    @Resource
    RedisUtil redisUtil;

    private ThreadLocal<Jedis> jedisThreadLocal=new ThreadLocal<>();

    private Jedis getJedis() {
        return redisUtil.getConnection();
    }

    public void set(String key, String value, int expireTimeVal) {

        Jedis jedis=null;
        try {
            jedis=jedisThreadLocal.get();
            if(null==jedis){
                jedis=getJedis();
                if(jedis!=null){
                    jedisThreadLocal.set(jedis);
                }
            }

            if (StringUtil.isNotEmpty(key) && StringUtil.isNotEmpty(value)) {
                jedisThreadLocal.get().set(prefix + key, value);
                //失效时间单位:秒
                if (expireTimeVal != 0) {
                    jedisThreadLocal.get().expire(prefix + key, expireTimeVal);
                } 

            }
        } catch (Exception e) {
            log.error("error is:", e);
        } finally {
            if (jedisThreadLocal.get() != null) {
                redisUtil.close(jedisThreadLocal.get());
                jedisThreadLocal.remove();
            }
        }

    }

    
    public void set(String key, String modelKey, String value, int time){
        set(modelKey + key, value, time);
    }

    public String get(String key) {
        Jedis jedis = null;
        String value = "";
        try {
            jedis=jedisThreadLocal.get();
            if(null==jedis){
                jedis=getJedis();
                if(jedis!=null){
                    jedisThreadLocal.set(jedis);
                }
            }

            if (StringUtil.isNotEmpty(key)) {
                if (jedisThreadLocal.get().exists(prefix + key)) {
                    value = jedisThreadLocal.get().get(prefix + key);
                }
            }
        } catch (Exception e) {
            log.error("error is:", e);
        } finally {
            if (jedisThreadLocal.get() != null) {
                redisUtil.close(jedisThreadLocal.get());
                jedisThreadLocal.remove();
            }
        }
        return value;

    }

    public void delete(String key) {
        Jedis jedis = null;
        try {
            jedis=jedisThreadLocal.get();
            if(null==jedis){
                jedis=getJedis();
                if(jedis!=null){
                    jedisThreadLocal.set(jedis);
                }
            }
            if (StringUtil.isNotEmpty(key)) {
                if (jedisThreadLocal.get().exists(prefix + key)) {
                    jedisThreadLocal.get().del(prefix + key);
                }
            }
        } catch (Exception e) {
            log.error("error is:", e);
        } finally {
            if (jedisThreadLocal.get() != null) {
                redisUtil.close(jedisThreadLocal.get());
                jedisThreadLocal.remove();
            }
        }

    }

}
